class Typing {
    constructor(str) {
        this.i = 0;
        this.str = "";
        this.copystr = "";
        this.timer = null;
        this.blink = false;
        this.init(str)
    }
    init(str) {
        if (str === undefined || str == "") return;
        this.copystr = str
        this.addType()
    }
    addType() {
        if (this.i <= this.copystr.length) {
            this.str = this.copystr.slice(0, this.i);
            if (this.i == this.copystr.length) {
                this.blink = true
                this.timer = setTimeout(() => {
                    this.blink = false
                    this.subType();
                }, 2000)
                return
            };
            this.i++;
            this.timer = setTimeout(() => {
                this.addType();
            }, 200)
        }
    }
    subType() {
        if (this.i >= 0) {
            this.str = this.copystr.slice(0, this.i);
            if (this.i == 0) {
                this.blink = true;
                this.timer = setTimeout(() => {
                    this.blink = false
                    this.addType()
                }, 2000)
                return;
            }
            this.i--;
            this.timer = setTimeout(() => {
                this.subType()
            }, 100)
        }
    }

    destroy() {
        clearTimeout(this.timer)
    }
}

export default Typing